﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    /// <summary>
    /// 按仓库选择商品
    /// </summary>
    public partial class MaterialStockPicker : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;
        Guid? _wareHouseId = null;
        Promise _promise = new Promise();

        public MaterialStockPicker()
        {
            InitializeComponent();
            this.ctlGridStock.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }
        public MaterialStockPicker(Guid? wareHouseId)
            : this()
        {
            this._wareHouseId = wareHouseId;
        }
        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", Resources.isOK, "确定", OnOKActionClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick))
                .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnCancelActionClick));
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                ctlSelectLookUpEditWareHouse.BindEWarehouse(_promise, _wareHouseId, true);
            })
            .Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (_wareHouseId != null)
                    {
                        ctlSelectLookUpEditWareHouse.Enabled = false;
                    }
                    else
                    {
                        ctlSelectLookUpEditWareHouse.Enabled = true;
                    }
                    _promise.Resolve();
                }));
            })
            .Then(RefreshData)
            .Run();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("按仓库选择商品");
        }
        #endregion

        #region  Method
        private int GetPageLimit()
        {
            return 25;
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        private void RefreshData()
        {
            Guid? wareHouseId = null;
            WebRef.VWarehouse item = this.ctlSelectLookUpEditWareHouse.GetSelectedValue<WebRef.VWarehouse>();
            if (item != null)
            {
                wareHouseId = item.Id;
            }
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VStockMaterial> result = new Service.QueryService().QueryStockMaterialPagedList(
                    this.txtFilter.Text.Trim(),
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    wareHouseId,
                    null,
                    null,
                    Domain.Constant.StockQueryTypes.All,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlGridStock.Paginate<WebRef.VStockMaterial>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                }));
            });
        }
        #endregion

        #region actions
        private void OnOKActionClick(object obj1, object obj2)
        {
            if (this.ctlGridViewStock.FocusedRowHandle > -1)
            {
                WebRef.VStockMaterial item = this.ctlGridViewStock.GetFocusedRow() as WebRef.VStockMaterial;
                if (item != null)
                {
                    UIContext.Current.ViewManager.CloseView(true, item);
                }
            }
        }
        private void OnRefreshActionClick(object obj1, object obj2)
        {
            _pageIndex = 0;
            RefreshData();
        }
        private void OnCancelActionClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            RefreshData();
        }
        /// <summary>
        /// 双击行选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlGridViewStock_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = ctlGridViewStock.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InRow)
                {
                    if (ctlGridViewStock.FocusedRowHandle > -1)
                    {
                        WebRef.VStockMaterial vStockMaterial = ctlGridViewStock.GetFocusedRow() as WebRef.VStockMaterial;
                        UIContext.Current.ViewManager.CloseView(true, vStockMaterial);
                    }
                }
            }
        }
        #endregion

    }
}
